#include "window.h"
#include <locale.h>
namespace client{

//-------------------构造函数中初始化屏幕-------------------------------
Window::Window()
{
  //设置编码格式，使其支持中文字符
  setlocale(LC_ALL,"");
  //初始化整个ncurses
  initscr();
  //隐藏光标（0为光标不可见，1为可见）
  curs_set(0);
}

//-----------------------析构函数销毁-----------------------------------
Window::~Window()
{
  //对整个ncurses进行销毁动作，若未销毁终端会显示混乱
  endwin();
}

//------------------------绘制标题窗口----------------------------------
void Window::DrawHeader()
{
  //需要设置窗口的高度宽度和横纵坐标
  
  //LINES和COLS是ncueses提供的宏，分别表示当前窗口的最大行数和列数
  int height=LINES/5;
  int width=COLS;
  int y=0;
  int x=0;
  header_win=newwin(height,width,y,x);
  //绘制窗口边框
  box(header_win,'|','-');
  //向标题窗口写上提示信息
  std::string title("My Chatting Room");
  PutStrToWin(header_win,height/2,width/2-title.size()/2,title);
  //需要进行刷新方可显示
  wrefresh(header_win);
}

//--------------------------绘制发送消息框窗口-------------------------------
void Window::DrawSendMsgBlank()
{
  int height=LINES/5;
  int width=COLS;
  int y=LINES/5*4;
  int x=0;
  sendmsg_win=newwin(height,width,y,x);
  box(sendmsg_win,'|','-');
  std::string inputstr="Please input your message:";
  PutStrToWin(sendmsg_win,1,2,inputstr);
  wrefresh(sendmsg_win);
}

//-------------------------绘制接收好友消息框窗口-------------------------------
void Window::DrawRecvMsgBlank()
{
  int height=LINES/5*3;
  int width=COLS/4*3;
  int y=LINES/5;
  int x=0;
  recvmsg_win=newwin(height,width,y,x);
  box(recvmsg_win,'|','-');
  std::string membermsg="【Member Message】";
  PutStrToWin(recvmsg_win,1,width/2-membermsg.size()/2,membermsg);
  for(size_t i=0;i<member_msgs.size();i++)
  {
    PutStrToWin(recvmsg_win,i+2,2,member_msgs[i]);  
  }
  wrefresh(recvmsg_win);
  
}

//----------------------绘制成员列表窗口----------------------------------
void Window::DrawMemberList()
{
  int height=LINES/5*3;
  int width=COLS/4;
  int y=LINES/5;
  int x=COLS*3/4;
  memberlist_win=newwin(height,width,y,x);
  box(memberlist_win,'|','-');
  std::string str="【Online Member】";
  PutStrToWin(memberlist_win,1,width/2-str.size()/2,str);
  //遍历成员列表，显示所有在线好友
  int i=0;
  for(auto item:member_list)//这里的item就是set中的元素，string类型
  {
    PutStrToWin(memberlist_win,i+2,1,item);
    i++;
  }
  wrefresh(memberlist_win);
}

//------------------------从窗口读字符串------------------------------------
void Window::GetStrFromWin(WINDOW* win,std::string* msg)
{
  char buf[1024*10]={0};
  wgetnstr(win,buf,sizeof(buf)-1);
  *msg=buf;
}

//-------------------------向窗口写字符串------------------------------------
void Window::PutStrToWin(WINDOW* win,int y,int x,const std::string str)
{
  //向某个窗口的固定位置输入字符串
  //这里的y和x的坐标是相对第一个参数对应的窗口而言的，表示相对位置
  mvwaddstr(win,y,x,str.c_str());
}

//----------------------向消息队列中添加好友消息------------------------------
void Window::AddMsg(const std::string& msg)
{
  //此处由于我们窗口显示的条数有限，不能无止境的插入消息
  //当member_msgs包含的消息数目超过一定阈值是，就删除旧消息
  member_msgs.push_back(msg);
  int max_line=LINES*3/5-4;
  if(max_line<3)
  {
    printf("lines too small,can't show message!\n");
    exit(0);
  }
  if((int)member_msgs.size()>max_line)
  {
    member_msgs.pop_front();
  }
}

//----------------------向在线好友列表中添加成员------------------------------
void Window::AddMember(const std::string& meminfo)
{
  member_list.insert(meminfo);
}

//-----------------------从成员列表中删除下线成员----------------------------
void Window::DelMember(const std::string& meminfo)
{
  member_list.erase(meminfo);
}
          


}

///////////////////////////////////////////////////////
// 对图形界面进行简单的测试
///////////////////////////////////////////////////////

#ifdef TEST_WINDOW
#include <unistd.h>
int main()
{
  client::Window win;
  win.DrawHeader();
  win.DrawSendMsgBlank();
  win.AddMsg("hahaha");
  win.AddMsg("hahaha");
  win.AddMsg("hahaha");
  win.AddMsg("hahaha");
  win.AddMsg("hahaha");
  win.DrawRecvMsgBlank();

  win.AddMember("sdt1<--->skd");
  win.AddMember("sdt2<--->skd");
  win.AddMember("sdt3<--->skd");
  win.AddMember("sdt4<--->skd");
  win.DrawMemberList();
  sleep(5);
  return 0;
}

#endif

